package base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.android.tu.loadingdialog.LoadingDailog;
import com.githang.statusbar.StatusBarCompat;
import com.hichip.campro.R;
import butterknife.ButterKnife;
import custom.dialog.Effectstype;
import custom.dialog.WarningDialogBuilder;
import utils.ActivityManager;
import utils.DialogUtils;
import utils.HiToast;

/**
 * 父类Activity
 */
public abstract class HiActivity extends FragmentActivity {

    public Dialog mJhLoading;
    public Dialog mLoadDialoging;
    public AlertDialog.Builder mDlgBuilder;
    private ActivityManager activitymanager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //绑定当前activity对象
        setContentView(setLayoutId());
        //设置状态栏为白色底 状态栏字体为黑色
        //FFFFFF 改成 00FFFFFF为了解决鱼眼横屏 魅族5顶部有白条
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#00FFFFFF"), true);
        ButterKnife.bind(this);
        init(savedInstanceState);
        activitymanager = ActivityManager.getInstance();
        activitymanager.addActivity(this);
    }

    /**
     * 设置资源布局
     */
    protected abstract int setLayoutId();

    protected abstract void init(@Nullable Bundle savedInstanceState);

//    @Override
//    public void finish() {
//        super.finish();
//        overridePendingTransition(R.anim.out_to_right, R.anim.in_from_left);
//    }
//
    public void finish(){
        super.finish();
        overridePendingTransition(R.anim.out_to_right, R.anim.in_from_left);
    }

    public  void finish(int value){
        super.finish();
        if(value==1){
            overridePendingTransition(R.anim.in_act_my, R.anim.act_anim_bot_out);
        }
    }


//    @Override
//    public void startActivityForResult(Intent intent, int requestCode) {
//        super.startActivityForResult(intent, requestCode);
//        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//    }

    public  void startActivityForResult(Intent intent,int requestCode){
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    public  void startActivityForResult(Intent intent,int requestCode,int value){
        super.startActivityForResult(intent, requestCode);
        if(value==1){
            overridePendingTransition(R.anim.act_anim_bot_in, R.anim.out_act_my);
        }else {
            overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
        }
    }


    public void  startActivity(Intent intent,boolean isBotttomOut){
        super.startActivity(intent);
        if(isBotttomOut){
            overridePendingTransition(R.anim.in_act_my, R.anim.act_anim_bot_out);
        }else {
            overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
        }
    }
    public void  startActivity(Intent intent){
        super.startActivity(intent);
        //overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
        overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
    }

    public void  startActivity(Intent intent,int value){
        super.startActivity(intent);
        if(value==1){
            overridePendingTransition(R.anim.act_anim_bot_in, R.anim.out_act_my);
        }else {
            overridePendingTransition(R.anim.in_act_my, R.anim.out_act_my);
        }
    }

    public void showAlert(CharSequence msg) {
        WarningDialogBuilder builder = WarningDialogBuilder.getInstance(this);
        builder.withMessage(msg).isCancelable(true).isCancelableOnTouchOutside(true).withEffect(Effectstype.Shake);
        builder.show();
    }

    /**
     * 菊花转的dialog
     */
    public void showjuHuaDialog() {
        if (mJhLoading == null) {
            mJhLoading = DialogUtils.createLoadingDialog(this, true);
        }
        if(!this.isFinishing()){
            mJhLoading.show();
        }
    }

    /**
     * 菊花转的dialog
     */
    public void dismissjuHuaDialog() {
        if (mJhLoading != null) {
            mJhLoading.dismiss();
        }
    }

    // loading  加载框显示
    public void showLoadDialog(String msg, boolean isOutside, boolean isCance) {
        Log.i("tedu","--showLoadDialog--:");
        if(mLoadDialoging==null){
            LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(this).setMessage(msg).setCancelable(isCance).setCancelOutside(isOutside);
            mLoadDialoging = loadBuilder.create();
            if(!this.isFinishing()){
                mLoadDialoging.show();
            }
        }else {
            if(!this.isFinishing()){
                mLoadDialoging.show();
            }
        }
    }

    // loading  加载框隐藏
    public void dissmissLoadDialog() {
        if (mLoadDialoging != null) {
            mLoadDialoging.dismiss();
            mLoadDialoging=null;
        }
    }


    public void showYesNoDialog(int msg, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(HiActivity.this);
        builder.setMessage(getResources().getString(msg)).setTitle(R.string.tips_warning).setPositiveButton(getResources().getString(R.string.btn_yes), listener).setNegativeButton(getResources().getString(R.string.btn_no), listener).show();

    }

    public void showAlertnew(Drawable iconId, CharSequence title, CharSequence message, CharSequence cancel, CharSequence okch, DialogInterface.OnClickListener listener) {
        if (mDlgBuilder == null) {
            mDlgBuilder = new AlertDialog.Builder(this);
            mDlgBuilder.setIcon(iconId);
            mDlgBuilder.setTitle(title);
            mDlgBuilder.setMessage(message);
            mDlgBuilder.setPositiveButton(okch, listener).setNegativeButton(cancel, listener).show();
        }
    }

}
